package com.yaocw.baseproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yaocw.baseproject.R;
import com.yaocw.baseproject.entity.HomeListItemEntity;

import java.util.List;

public class HomeListAdapter extends RecyclerView.Adapter {

    private final Context mContext;
    private List<HomeListItemEntity> mDatas;

    public HomeListAdapter(Context mContext, List<HomeListItemEntity> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_list_item, parent, false);
        return new HomeListItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HomeListItemViewHolder vh = (HomeListItemViewHolder) holder;
        vh.tvTitle.setText(mDatas.get(position).getTitle());
        vh.ivAvatar.setImageResource(mDatas.get(position).getImageUrl());
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class HomeListItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private ImageView ivAvatar;

        public HomeListItemViewHolder(@NonNull View itemView) {
            super(itemView);

            tvTitle = itemView.findViewById(R.id.tv_title);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
        }
    }
}
